using ED.Common;
using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.Common.Utils;
using ED.Entity;
using ED.IRepository;
using ED.IService;
using ED.Model.Request;
using ED.Model.Response;

/// <summary>
/// Role服务
/// </summary>
namespace ED.Service
{
    public class RoleService : BaseService, IRoleService
    {
        private readonly IRoleRepository roleRepository;
        public RoleService(IRoleRepository roleRepository)
        {
            this.roleRepository = roleRepository;
        }

        /// <summary>
        /// Role添加
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Add(RoleOpearteReq req)
        {
            // 验证 后续补上
            var roleEntity = req.MapTo<Base_RoleEntity>();
            var res = roleRepository.Insert(roleEntity);
            return res.IsDefault() ? new Tuple<bool, string>(false, "添加角色失败") : new Tuple<bool, string>(true, "添加角色成功");
        }

        /// <summary>
        /// Role删除
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Delete(int req)
        {
            var res = roleRepository.Delete(req);
            return res > 0 ? new Tuple<bool, string>(true, "删除成功") : new Tuple<bool, string>(false, "删除失败");
        }

        /// <summary>
        /// Role查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageOut<RoleRes> Page(RoleReq req)
        {
            return roleRepository.GetList(t => t.IsDeleted == false).Mapper<List<RoleRes>>().GetPageOut(req.PageIndex, req.PageSize);
        }

        /// <summary>
        /// Role更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(RoleOpearteReq req)
        {
            var resEntity = roleRepository.Get(t => t.Id == req.Id);
            if (resEntity.IsDefault())
            {
                ThrowHelp.Throw("该角色不存在");
            }
             
            var entity = resEntity.Mapper<Base_RoleEntity>();
            var res = roleRepository.Update(entity);
            return res.IsDefault() ? new Tuple<bool, string>(false, "更新角色失败") : new Tuple<bool, string>(true, "更新角色成功");
        }
    }
}
